package cn.edu.buaa.cnsatm.traffic_big_data_server.Util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author: LX
 * @className: DateUtil
 * @packageName:
 * @description: 对日期的操作
 * @data: 2019-10-12
 **/
public class DateUtil{
    public static List<String> traversalDateBetweenTwoDate(Date dateLowerBound, Date dateUpperBound){
        /**
         * @author:  LX
         * @methodsName: traversalDateBetweenTwoDate
         * @description: 遍历两个日期间的所有日期（年月日）
         * @return: 存放日期参数间的所有日期，以List<String>形式返回
         */
        List<String> dateList = new ArrayList<String>();
        /*这里只需要年月日*/
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        /*使用日期可以一天一天增加的Calendar*/
        Calendar dateLowerBoundCalendar = Calendar.getInstance();
        Calendar dateUpperBoundCalendar = Calendar.getInstance();
        /*用Date对Calendar初始化*/
        dateLowerBoundCalendar.setTime(dateLowerBound);
        dateUpperBoundCalendar.setTime(dateUpperBound);
        /*将Calendar转为字符串，便于存储，判等*/
        String dateLowerBoundString = formatter.format(dateLowerBoundCalendar);
        String dateUpperBoundString = formatter.format(dateUpperBoundCalendar);
        while(!(dateLowerBoundString.equals(dateUpperBoundString))){
            /*数据表名没有-，所以存的时候给去掉*/
            dateList.add(dateLowerBoundString.replaceAll("-",""));
            dateLowerBoundCalendar.add(Calendar.DATE, 1);
            dateLowerBoundString = formatter.format(dateLowerBoundCalendar);
        }
        return dateList;
    }
}